// TypeScript file

class Enemy extends egret.Sprite
{
    public row: number = 0;
    public name: string = "";
    public over: boolean = false;
    public stopMove: boolean = false;

    public isStopHasClick:boolean = false;
    private sp: StarlingSwfMovieClip;
    public blod: number = 0;
    public guo: number = 0;

    public type: number = 0;

    private onJump: boolean = false;

    constructor(type: number)
    {
        super();
        this.initView(type);
    }

    private initView(type: number): void
    {
        this.type = type;
        this.blod = 1;
        switch (type)
        {
            case EnemyType.lang:
            {
                this.sp = StarlingSwfFactory.getInstance().makeMc("lang");
                this.sp.gotoAndPlay("run");
            }
            break;
            case EnemyType.lieren:
            {
                this.sp = StarlingSwfFactory.getInstance().makeMc("lieren");
                this.sp.gotoAndPlay("run");
            }
            break;
            case EnemyType.huli:
            {
                this.sp = StarlingSwfFactory.getInstance().makeMc("huli");
                this.sp.gotoAndPlay("run");
                this.blod = 2;
            }
            break;
            case EnemyType.niao:
            {
                this.sp = StarlingSwfFactory.getInstance().makeMc("niao");
                this.sp.gotoAndPlay("run");
            }
            break;
            case EnemyType.bianfu:
            {
                this.sp = StarlingSwfFactory.getInstance().makeMc("bianfu");
                this.sp.gotoAndPlay("run");
                this.blod = 3;
            }
            break;
            case EnemyType.daoju1:
            {
                this.sp = StarlingSwfFactory.getInstance().makeMc("daoju");
                this.sp.gotoAndPlay("1");
            }
            break;
            case EnemyType.daoju2:
            {
                this.sp = StarlingSwfFactory.getInstance().makeMc("daoju");
                this.sp.gotoAndPlay("2");
            }
            break;
            case EnemyType.daoju3:
            {
                this.sp = StarlingSwfFactory.getInstance().makeMc("daoju");
                this.sp.gotoAndPlay("3");
            }
            break;
            case EnemyType.daoju4:
            {
                this.sp = StarlingSwfFactory.getInstance().makeMc("daoju");
                this.sp.gotoAndPlay("4");
            }
            break;
        }

        this.addChild(this.sp);
    }

    public alphaToZero(): void
    {
        this.visible = false;
    }

    public gotoJump(): void
    {
        this.sp.goToPlay("jump");
    }

    public gotoStop(): void
    {
        this.sp.gotoAndStop(0);
    }
    
    public gotoDie(): void
    {
        this.sp.gotoAndPlay("die");
    }

    public move():void
    {
        if (this.stopMove)
        {
            this.goOut();
        }

        if (this.y >= 820)
        {
            this.y = 820;
            this.over = true;
            return;
        }

        if (!this.onJump)
        {
            if (!this.stopMove)
            {
                this.y += GameData.enemySpeed;
            }
            return;
        }

        this.y -= GameData.enemySpeed * 3;
        this.gotoJump();
        this.isStopHasClick = true;

        if (this.y > 160) return;

        this.sp.gotoAndPlay("run");
        this.onJump = false;
        this.isStopHasClick = false;

        if (!(this.type == 4 || this.type == 5)) return;

        let bb: number = Math.floor(Math.random() * 4 + 1);
        if (bb != 1) return;

        switch (this.x)
        {
            case 10:
            {
                this.x = 129;
            }
            break;
            case 129:
            {
                this.x = 248;
            }
            break;
            case 248:
            {
                this.x = 129;
            }
            break;
            case 367:
            {
                this.x = 248;
            }
            break;
        }
    }

    private goOut(): void
    {
        if (this.row == 1)
        {
            this.x -= 15;
            this.y -= 15;
        }
        else if (this.row == 2)
        {
            this.x -= 15;
            this.y -= 20;
        }
        else if (this.row == 3)
        {
            this.x += 15;
            this.y -= 20;
        }
        else if (this.row == 4)
        {
            this.x += 15;
            this.y -= 15;
        }

    }

    public stopMoveEnemy(): void
    {
        this.stopMove = true;
    }

    public dispose(): void
    {
        this.removeChildren();
    }
}

class EnemyType
{
    public static lang: number = 1;
    public static lieren: number = 2;
    public static huli: number = 3;
    public static niao: number = 4;
    public static bianfu: number = 5;
    public static daoju1: number = 6;
    public static daoju2: number = 7;
    public static daoju3: number = 8;
    public static daoju4: number = 9;
}